package main;

import com.primavera.common.value.ObjectId;
import main.Link.LinkTable;
import main.Link.SQLLink;
import main.SQL.FillSQLSmetModel;
import main.Trees.SmetNode;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

import java.text.NumberFormat;
import java.util.ArrayList;

/**
 * Created by s_shmakov on 25.04.14.
 */
public class SKTreeModel extends AbstractTreeTableModel {
    private SmetNode root;
    NumberFormat formatter;
    private LinkTable lTable = null;
    private ObjectId curProj;

    Class[] types = new Class[]{
            Object.class, Object.class, Object.class, Double.class, Double.class, Double.class, Double.class, Double.class, Double.class, boolean.class, Object.class
    };

    public void openSmet(SmetNode node) {
        this.root = node;
    }

    boolean[] canEdit = new boolean[]{false, false, false, false, false, false, false, true};

    public SmetNode getSmRow(int id, SmetNode node) {
        SmetNode stn = null;
        for (SmetNode sm : node.getChildren()) {
            if (sm.getId() == id) return sm;
            if (!sm.isString) stn = getSmRow(id, sm);
            if (stn != null) return stn;
        }
        return stn;
    }

    public void closeSQLSmet(SmetNode node) {
        root.getChildren().remove(node);
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        return types[columnIndex];
    }

    public SKTreeModel() {
        root = new SmetNode("Все открытые сметы");
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "N";
            case 1:
                return "Наименование";
            case 2:
                return "Дата начала";
            case 3:
                return "Дата окончания";
//            case 4:
//                return "Стоимость";
            default:
                return "Unknown";
        }
    }

    @Override
    public Object getValueAt(Object node, int column) {
        SmetNode treenode = (SmetNode) node;
        switch (column) {
            case 0:
                return treenode.getNum();
            case 1:
                return treenode.getName();
            case 2:
                return treenode.getUnit();
            case 3:
                return treenode.getSmNum();
//            case 4:
//                return treenode.getCost();
            default:
                return "Unknown";
        }
    }

    @Override
    public Object getChild(Object node, int index) {
        SmetNode treenode = (SmetNode) node;
        return treenode.getChildren().get(index);
    }

    @Override
    public int getChildCount(Object parent) {
        SmetNode treenode = (SmetNode) parent;
        return treenode.getChildCount();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        SmetNode treenode = (SmetNode) parent;
        for (int i = 0; i > treenode.getChildCount(); i++) {
            if (treenode.getChildren().get(i) == child) {
                return i;
            }
        }

        return 0;
    }

    @Override
    public boolean isLeaf(Object node) {
        SmetNode treenode = (SmetNode) node;
        if (treenode.getChildCount() > 0) {
            return false;
        }
        return true;
    }

    @Override
    public SmetNode getRoot() {
        return root;
    }
}
